var express = require('express');
var router = express.Router();

var user = require('../models/user');
var goods = require('../models/goods');
var slideshow = require('../models/slideshow');
var error = require('../models/error');

// 管理员用户登录
router.post('/login', function (req, res, next) {
  if (!req.body.username) {
    res.json({ status: 1, message: "管理员用户名为空" });
  }
  if (!req.body.password) {
    res.json({ status: 1, message: "管理员密码为空" });
  }
  user.findAdminLogin(req.body.username, req.body.password, function (err, userSave) {
    if (userSave.length != 0) {
      res.json({ status: 0, message: "用户登录成功", data: userSave })
    } else {
      res.json({ status: 1, message: "用户名或者密码错误或者没有权限", data: err })
    }
  })
})

// 查找所有用户
router.get('/findAll', function (req, res, next) {
  user.findAll(function (err, data) {
    if (err) {
      res.json({ status: 1, message: "获取失败", data: err });
    } else {
      res.json({ status: 0, message: "获取成功", data: data });
    }
  })
})

// 根据用户名查找用户
router.post('/findOne', function (req, res, next) {
  user.findByUsername(req.body.username, function (err, getUser) {
    if (err) {
      res.json({ status: 1, message: "获取错误", data: err });
    } else {
      res.json({ status: 0, message: "获取成功", data: getUser });
    }
  })
})

// 更改用户信息
router.post('/editUser', function (req, res, next) {
  let obj = {
    userPhone: req.body.userPhone,
    userMail: req.body.userMail,
    password: req.body.password,
    userStop: req.body.userStop,
    userAdmin: req.body.userAdmin
  }
  user.update({ _id: req.body._id }, obj, function (err, data) {
    if (err) {
      res.json({ status: 1, message: "更新失败", data: err });
    } else {
      res.json({ status: 0, message: "更新成功", data: data });
    }
  })
})

// 创建轮播图
router.post('/createSlide', function (req, res, next) {
  if (!req.body.imagesList) {
    res.json({ status: 1, message: "轮播图为空" });
  }
  slideshow.findByname(req.body.name, function (err, data) {
    if (data) {
      res.json({ status: 1, message: "已存在轮播图", data: data });
    } else {
      const createSlide = new slideshow({
        name: 'slideshow',
        imagesList: req.body.imagesList
      })
      createSlide.save(function (err, data) {
        if (err) {
          res.json({ status: 1, message: "创建失败", data: err });
        } else {
          res.json({ status: 0, message: "创建成功", data: data });
        }
      })
    }
  })
})

// 查找轮播图信息
router.post('/findSlide', function (req, res, next) {
  slideshow.findByname(req.body.name, function (err, data) {
    if (data) {
      res.json({ status: 0, message: "查找成功", data: data });
    } else {
      res.json({ status: 1, message: "查找失败" });
    }
  })
})

// 更改轮播图信息
router.post('/editSlide', function (req, res, next) {
  slideshow.update({ _id: req.body._id }, { imagesList: req.body.imagesList }, function (err, data) {
    if (err) {
      res.json({ status: 1, message: "更改失败", data: err });
    } else {
      res.json({ status: 0, message: "更改成功", data: data });
    }
  })
})

// 错误监控
router.post('/postError', function (req, res, next) {
  const err = new error({
    type: req.body.type,
    error: req.body.error
  })
  err.save(function () {
    res.json({ status: 0, message: '上传成功' })
  })
})
router.get('/getError', function (req, res, next) {
  error.findAll(function (err, errorList) {
    if (err) {
      res.json({ status: 1, message: '查找失败', data: err });
    } else {
      res.json({ status: 0, message: '获取成功', data: errorList });
    }
  })
})

module.exports = router;